package com.shanghaionstar.discover.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.onstar.cn.more.entity.MyOVD;
import com.shanghaionstar.R;

import java.util.List;

public class OvdEmailAdapter extends BaseExpandableListAdapter {
    private List<MyOVD> ovds;

    public OvdEmailAdapter(Context context, List<MyOVD> ovds) {
        this.mContext = context;
        this.ovds = ovds;
    }

    private Context mContext;


    public Object getChild(int groupPosition, int childPosition) {
        MyOVD ovd = ovds.get(groupPosition);
        String des = null;
        switch (ovd.getStatus()) {
            case 0:
                des = "";
                break;
            default:
                des = ovd.getDescription();
                break;
        }
        return des;
    }

    public long getChildId(int groupPosition, int childPosition) {
        return 1;
    }

    public int getChildrenCount(int groupPosition) {
        MyOVD ovd = ovds.get(groupPosition);
        int childCount = -1;
        switch (ovd.getStatus()) {
            case 0:
                childCount = 0;
                break;
            default:
                childCount = 1;
                break;
        }
        return childCount;
    }

    class ChildHolder {
        TextView tv;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.discover_ovd_condition_children_item,
                    null);
            holder = new ChildHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.child_tv);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        holder.tv.setText(ovds.get(groupPosition).getDescription());
        return convertView;
    }

    public Object getGroup(int groupPosition) {
        return ovds.get(groupPosition);
    }

    public int getGroupCount() {
        return ovds.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    class ViewHolder {
        TextView titleTv;
        TextView contentTv;
        ImageView arrowIv;
        ImageView stateIv;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.discover_ovd_condition_group_item, null);
            holder = new ViewHolder();
            holder.titleTv = (TextView) convertView.findViewById(R.id.item_title_tv);
            holder.contentTv = (TextView) convertView.findViewById(R.id.item_content_tv);
            holder.arrowIv = (ImageView) convertView.findViewById(R.id.arrow_iv);
            holder.stateIv = (ImageView) convertView.findViewById(R.id.state_iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MyOVD ovd = ovds.get(groupPosition);
        if (holder != null) {
            int resId = getStatusDrawableId(ovd.getStatus());
            holder.stateIv.setImageDrawable(mContext.getResources().getDrawable(resId));
            if (ovd.getStatus() == 0) {
                holder.arrowIv.setVisibility(View.GONE);
            } else {
                holder.arrowIv.setVisibility(View.VISIBLE);
                if (isExpanded) {
                    holder.arrowIv.setImageResource(R.drawable.gray_arrow_up);
                } else {
                    holder.arrowIv.setImageResource(R.drawable.gray_arrow_down);
                }
            }
            holder.titleTv.setText(ovd.getTitle());
            holder.contentTv.setText(getOVDContent(ovd.getStatus()));
        }
        return convertView;
    }

    /**
     * @Title: getOVDContent @Description: TODO @param @param
     * getmStatus @param @return 设定文件 @return CharSequence 返回类型 @throws
     */
    private String getOVDContent(int status) {
        if (status == 0) {
            return mContext.getString(R.string.goodContent);
        } else if (status == 1) {
            return mContext.getString(R.string.adequateContent);
        } else {
            return mContext.getString(R.string.criticalContent);
        }
    }

    /**
     * @Title: getStatusDrawableId @Description: TODO @param @param
     * getmStatus @param @return 设定文件 @return int 返回类型 @throws
     */
    private int getStatusDrawableId(int status) {
        if (status == 0) {
            return R.drawable.v_c_c;
        } else if (status == 1) {
            return R.drawable.v_c_p;
        } else {
            return R.drawable.v_c_u;
        }
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }
}
